package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/checkItem")
public class CheckItemController {

    @Reference
    private CheckItemService checkItemService;

    //添加检查项目
    @RequestMapping("/add")
    @ResponseBody
    @PreAuthorize("hasAnyRole('ROLE_ADMIN')")
    public Result add(@RequestBody CheckItem checkItem){


        System.out.println("===============================checkItem = " + checkItem);

        try{
            checkItemService.addCheckItem(checkItem);

        }catch (Exception e){

            return new Result(false, MessageConstant.ADD_CHECKITEM_FAIL);
        }
            return  new Result(true,MessageConstant.ADD_CHECKITEM_SUCCESS);
    }
    //添加检查项目
    @RequestMapping("/findPage")
    @ResponseBody
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){

            PageResult pageResult = checkItemService.findPage(queryPageBean);

       // System.out.println("===============================queryPageBean = " + queryPageBean);

        return pageResult;

    }

    //删除检查项目
    @RequestMapping("/delete/{id}")
    @ResponseBody
    public Result delete(@PathVariable Integer id){
        System.out.println("===============================id = " + id);
        try {
            checkItemService.deleteCheckItem(id);

        }catch(Exception e){
            return new Result(false,MessageConstant.DELETE_CHECKITEM_FAIL);
        }
        //删除成功信息
        return new Result(true,MessageConstant.DELETE_CHECKITEM_SUCCESS);
    }

    //根据id查找
    @RequestMapping("/findById")
    @ResponseBody
    public Result findById(Integer id){
        //System.out.println("===============================id = " + id);

        try {
            CheckItem checkItem = checkItemService.findById(id);
            //编辑检查信息成功
            return new Result(true,MessageConstant.EDIT_CHECKITEM_SUCCESS,checkItem);
        }catch(Exception e){

            return new Result(false,MessageConstant.EDIT_CHECKITEM_FAIL);
        }
    }
    //编辑信息
    @RequestMapping("/update")
    @ResponseBody
    public Result update(@RequestBody CheckItem checkItem){
        System.out.println("===============================checkItem = " + checkItem);

        try {
             checkItemService.update(checkItem);
            //编辑检查信息成功
            return new Result(true,MessageConstant.EDIT_CHECKITEM_SUCCESS);
        }catch(Exception e){

            return new Result(false,MessageConstant.EDIT_CHECKITEM_FAIL);
        }
    }
    //查询检查组的所有信息信息
    @RequestMapping("/findAll")
    @ResponseBody
    public Result findAll(){

        try {
            List<CheckItem> checkItemList = checkItemService.findAll();

            System.out.println("===============================checkItemList = " + checkItemList);

            return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,checkItemList);
        }catch(Exception e){

            return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

}
